﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using WindowsGame.Library.Data;

namespace WindowsGame.Library.ProxyObjects
{
	public class ProxyObjectBuilder
	{
		private IFileManager fileManager;
		private IProxyObjectManager proxyObjectManager;

		public ProxyObjectBuilder(IFileManager fileManager, IProxyObjectManager proxyObjectManager)
		{
			this.fileManager = fileManager;
			this.proxyObjectManager = proxyObjectManager;
		}

		public void BuildProxyObjects(String fldr, String txtFile)
		{
			proxyObjectManager.Clear();

			IList<String> xmlFiles = fileManager.LoadTxt(fldr + txtFile);
			BuildProxyObjects(fldr, xmlFiles);
		}

		public void BuildProxyObjects(String fldr, IList<String> xmlFiles)
		{
			proxyObjectManager.Clear();
			foreach (String xmlFile in xmlFiles)
			{
				XElement root = fileManager.LoadXElement(fldr + xmlFile);
				proxyObjectManager.Parse(root, xmlFile);
			}
		}

		public ProxyObject GetProxyObject(String name)
		{
			ProxyObject proxyObject;
			Boolean success = proxyObjectManager.ProxyObjects.TryGetValue(name, out proxyObject);
			return success ? proxyObject : null;
		}

		public String GetProxyName(String name)
		{
			String proxyName;
			Boolean success = proxyObjectManager.ProxyNames.TryGetValue(name, out proxyName);
			return success ? proxyName : null;
		}

		public IProxyObjectManager ProxyObjectManager { get { return proxyObjectManager; } }
	}
}